<?php
class Delivery_Model_DbTable_Mails
	extends Delivery_Model_DbTable_Abstract

{
	protected $_name = '_mails';
	protected $_primary = 'id';
	
	protected function _select( $count = false, $params, $offset = null, $limit = null, $order = null )
	{
		
		$select = $this->select();

		if( $count ){
			$select->from($this, array('count' => 'COUNT(1)'));
		}
		
		if ( isset($params['nombre']) ){
			$select->where( 'nombre LIKE ?', "%{$params['nombre']}%" );
		}
		
		if ( isset($params['descripcion']) ){
			$select->where( 'descripcion LIKE ?', "%{$params['descripcion']}%" );
		}
		
		if ( isset($params['email']) ){
			$select->where( 'email LIKE ?', "%{$params['email']}%" );
		}
		
		if ( isset($params['url']) ){
			$select->where( 'url LIKE ?', "%{$params['url']}%" );
		}
		
		if ( isset($params['calle']) ){
			$select->where( 'calle LIKE ?', "%{$params['calle']}%" );
		}
		
		if ( isset($params['barrio']) ){
			$select->where( 'barrio LIKE ?', "%{$params['barrio']}%" );
		}
		
		
		if ( $order ){
			$select->order( $order );
		}
		
		if ( $limit && !is_null($offset) ){
			$select->limit( $limit, $offset );
		}
		
		return $select;
		
	}
		
	
	public function getByIds( $ids){
		
		$ids = is_array($ids) ? $ids : explode(",", $ids);
		
		$select = $this->select()->where('id IN (?)', $ids, Zend_Db::INT_TYPE);

		return $this->_db->fetchAll($select, null, Zend_Db::FETCH_OBJ );
	}
	
	public function getByParams( $params, $offset = null, $limit = null, $order = null )
	{
		$select = $this->_select( false, $params, $offset, $limit, $order);
		
		return $this->_db->fetchAll($select, null, Zend_Db::FETCH_OBJ );
	}
		
	public function countByParams( $params, $offset = null, $limit = null, $order = null )
	{
		$select = $this->_select( true, $params, 0, 1, null);
		$result = $this->_db->fetchRow($select);
		return $result['count'];
	}
	
	public function setEnviado($id)
	{
		return $this->update(
			array('enviado' => date("Y-m-d")),
			array('id = ?' => $id)
		);
	}
	
}